package org.smartboot.sosa;

import java.util.concurrent.Executors;

import com.lmax.disruptor.EventFactory;
import com.lmax.disruptor.EventHandler;
import com.lmax.disruptor.EventTranslatorOneArg;
import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.dsl.Disruptor;

public class DisrutproDemo {
	public static void main(String[] args) throws InterruptedException {
		final EventTranslatorOneArg<ProcessUnit, String> TRANSLATOR = new EventTranslatorOneArg<ProcessUnit, String>() {
			@Override
			public void translateTo(ProcessUnit event, long sequence, String t) {
				event.msg = t;
			}
		};
		Disruptor<ProcessUnit> disruptor = new Disruptor<ProcessUnit>(new ProcessUnitFactory(), 1024,
			Executors.newCachedThreadPool());
		disruptor.handleEventsWith(new ProcessUnitEventHandler());
		final RingBuffer<ProcessUnit> ringBuffer = disruptor.start();
		System.out.println(Thread.currentThread());
		Thread.sleep(1000);
		for (int i = 0; i < 1; i++) {
			Thread t = new Thread() {

				/*
				 * (non-Javadoc)
				 * 
				 * @see java.lang.Thread#run()
				 */
				@Override
				public void run() {
					while (true) {
						ringBuffer.publishEvent(TRANSLATOR, "ad" + Thread.currentThread());

						try {
							Thread.sleep(1000);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}

			};
			t.setPriority(10);
			t.start();
		}
	}

}

class ProcessUnit {
	public String msg;
}

class ProcessUnitEventHandler implements EventHandler<ProcessUnit> {
	public void onEvent(ProcessUnit unit, long sequence, boolean endOfBatch) {
		System.out.println(Thread.currentThread() + "处理消息" + unit.msg);

	}
}

class ProcessUnitFactory implements EventFactory<ProcessUnit> {
	public ProcessUnit newInstance() {
		return new ProcessUnit();
	}
}
